<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\model;
use think\Db;
use think\Request;

class Auths
{
    /**验证权限
     * @param Request $request
     */
    static function login_auth($modelid){
        $data = session('adminuser');
        if(!$data){
            return false;
        }
        $role = $data['role'];
        $rote = Db::name('rote')->where('id',$role)->find();
        $hson = json_decode($rote['pri_id_list'],true);
        $array = Db::name('privilege')->where('id','in',$hson)->select();
        $ar =[];
        foreach ($array  as $value){
            $ar[]=$value['jur'];
        }
        $model = Db::name('model')->where('id',$modelid)->find();
        if(in_array($model['surface'],$ar)){
            return true;
        }else{
            return false;
        }
    }

    /**用documentid判断
     * @param $documentid
     * @return bool
     */
    static  function dataid_auth($documentid){
        $modelid = (Db::name('document')->where('id',$documentid)->find())['model'];
        return Auths::login_auth($modelid);
    }

    /**单表判断
     * @param $table：表名称
     * @return bool
     */
    static function table_auth($table){
        $data = session('adminuser');
        if(!$data){
            return false;
        }
        $role = $data['role'];
        $rote = Db::name('rote')->where('id',$role)->find();
        $hson = json_decode($rote['pri_id_list'],true);
        $array = Db::name('privilege')->where('id','in',$hson)->select();
        foreach ($array  as $value){
            $ar[]=$value['jur'];
        }
        if(in_array($table,$ar)){
            return true;
        }else{
            return false;
        }
    }
}